# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=jasper

pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=1.900.1
pkgrel=2
pkgdesc="A software-based implementation of the codec specified in the emerging JPEG-2000 Part-1 standard (mingw-w64)"
arch=('any')
url="http://www.ece.uvic.ca/~mdadams/jasper"
license=("custom:JasPer2.0")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc" "${MINGW_PACKAGE_PREFIX}-pkg-config" "dos2unix")
depends=("${MINGW_PACKAGE_PREFIX}-libjpeg-turbo")
options=('staticlibs' 'strip' '!libtool')
source=("http://www.ece.uvic.ca/~mdadams/jasper/software/jasper-${pkgver}.zip"
    "jpc_dec.c.patch"
    "patch-libjasper-stepsizes-overflow.diff"
    "jasper-1.900.1-CVE-2008-3520.patch"
    "jasper-1.900.1-CVE-2008-3522.patch"
    "jasper-1.900.1-mingw32.patch"
    "jasper-1.900.1-canonical-target.patch"
    "jasper-1.900.1-enable-shared.patch"
    "jasper-1.900.1-sleep.patch")
sha1sums=('9c5735f773922e580bf98c7c7dfda9bbed4c5191'
          'c1a0176a15210c0af14d85e55ce566921957d780'
          'f298566fef08c8a589d072582112cd51c72c3983'
          '2483dba925670bf29f531d85d73c4e5ada513b01'
          '0e7b6142cd9240ffb15a1ed7297c43c76fa09ee4'
          'ff3bd0235915b89020d345db96e879f669d5e473'
          'ec93341267617d9d75eafc041f4786c16b07f62c'
          '54940e11d1b04964a0ed9329a8e1ffbc290353ad'
          'd009ecd9d1d0212bcedcf28f483db77ddd3bb1c7')

prepare() {
  cd "$srcdir/jasper-$pkgver"
  find . -type f -exec dos2unix {} \;
  patch -p1 -i "${srcdir}/jpc_dec.c.patch"
  patch -p1 -i "${srcdir}/patch-libjasper-stepsizes-overflow.diff"
  patch -p1 -i "${srcdir}/jasper-1.900.1-CVE-2008-3520.patch"
  patch -p1 -i "${srcdir}/jasper-1.900.1-CVE-2008-3522.patch"
  patch -p1 -i "${srcdir}/jasper-1.900.1-mingw32.patch"
  patch -p1 -i "${srcdir}/jasper-1.900.1-canonical-target.patch"
  patch -p1 -i "${srcdir}/jasper-1.900.1-enable-shared.patch"
  patch -p1 -i "${srcdir}/jasper-1.900.1-sleep.patch"
  
  libtoolize --copy --force
  aclocal
  automake --add-missing
  autoconf
}

build() {
  mkdir -p "${srcdir}/build-${MINGW_CHOST}"
  cd "${srcdir}/build-${MINGW_CHOST}"
  "${srcdir}"/${_realname}-${pkgver}/configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --enable-shared \
    --enable-static
  make
}

check() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  make check
}

package() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  make DESTDIR="$pkgdir" install
  find "${pkgdir}${MINGW_PREFIX}" -name '*.def' -o -name '*.exp' | xargs -rtl1 rm
}
